/**
 *
 */
package mainApp;

/**
 * @author Picto
 *
 */
import java.io.InputStream;
import java.net.*;
import java.util.*;
import javax.swing.JOptionPane;
import utils.DataAdapter;

public class Word {

    private Scanner inputScan = new Scanner(System.in);
    private Random randomGenerator = new Random();
    private ArrayList<String> wordList = new ArrayList<String>();
    private String word = "";
    private int wordLength = 0;
    private DataAdapter da = new DataAdapter();

    //TODO: Implement wordlist class
    public Word() {
        String selection;
        InputStream location = null;

        System.out.println("Please enter the location of your word file. Enter d to use the default file:");
        selection = inputScan.next();

        if (selection.equals("d") | selection.equals("D")) {
            location = this.getClass().getResourceAsStream("/wordLists/wordList.txt");
            wordList = da.getWordList(location);
            getNewWord();
        } else {
            location = this.getClass().getResourceAsStream(selection);

            wordList =  da.getWordList(location);
            getNewWord();
        }
    }

    public Word(String selection) {
        if (selection.equals("Default")) {
            InputStream location = null;
            location = this.getClass().getResourceAsStream("/wordLists/wordList.txt");
            wordList = da.getWordList(location);
            getNewWord();
        } else {
            wordList =  da.getWordList(selection);
            getNewWord();
        }
    }

    public String getValue() {
        return word;
    }

    public int getWordLength() {
        return wordLength;
    }

    private void setWordLength() {
        wordLength = word.length();
    }

    public void getNewWord() {
        int listLength = wordList.size();
        if (listLength != 0) {
            word = wordList.get(randomGenerator.nextInt(listLength));
            setWordLength();
        } else {
            JOptionPane.showMessageDialog(null, "The word list selected is empty!");
        }
    }
}
